package com.cssy.xxkj.basic.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cssy.xxkj.basic.dao.BasicCompanyTestDAO;
import com.cssy.xxkj.basic.dao.BasicCurrencyTestDAO;
import com.cssy.xxkj.basic.dao.BasicMaterialQualityDAO;
import com.cssy.xxkj.basic.dao.BasicModePaymentTestDAO;
import com.cssy.xxkj.basic.dao.BasicProcessDAO;
import com.cssy.xxkj.basic.dao.BasicSeriesDAO;
import com.cssy.xxkj.basic.dao.BasicStapleUnitDAO;
import com.cssy.xxkj.basic.dao.BasicStorageInfoTestDAO;
import com.cssy.xxkj.basic.dao.BasicWorkshopDAO;
import com.cssy.xxkj.basic.entity.BasicCompanyTestDO;
import com.cssy.xxkj.basic.entity.BasicCurrencyTestDO;
import com.cssy.xxkj.basic.entity.BasicMaterialQualityDO;
import com.cssy.xxkj.basic.entity.BasicModePaymentTestDO;
import com.cssy.xxkj.basic.entity.BasicProcessDO;
import com.cssy.xxkj.basic.entity.BasicSeriesDO;
import com.cssy.xxkj.basic.entity.BasicStapleUnitDO;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.plat.helper.SearchConditions;


@Service("basicUtilService")
public class BasicUtilServiceImpl implements IBasicUtilService{
	//仓库DAO
	@Resource(name="basicStorageInfoTestDAO")	
	private BasicStorageInfoTestDAO basicStorageInfoTestDAO;
	//基本单位DAO
	@Resource(name="basicStapleUnitDAO")	
	private BasicStapleUnitDAO basicStapleUnitDAO;
	//仓库DAO
	@Resource(name="basicProcessDAO")	
	private BasicProcessDAO basicProcessDAO;
	//车间DAO
	@Resource(name="basicWorkshopDAO")	
	private BasicWorkshopDAO basicWorkshopDAO;
	//材质DAO
	@Resource(name="basicMaterialQualityDAO")	
	private BasicMaterialQualityDAO basicMaterialQualityDAO;
	//所有客户
	@Resource(name="basicCompanyTestDAO")	
	private BasicCompanyTestDAO basicCompanyTestDAO;
	//所有币种
	@Resource(name="basicCurrencyTestDAO")	
	private BasicCurrencyTestDAO basicCurrencyTestDAO;
	
	//所有币种
	@Resource(name="basicSeriesDAO")	
	private BasicSeriesDAO basicSeriesDAO;
	
	//所有付款方式
	@Resource(name="basicModePaymentTestDAO")	
	private BasicModePaymentTestDAO basicModePaymentTestDAO;
	
	@Override
	public List<BasicStorageInfoTestDO> resultStorage(SearchConditions scs) {
		return basicStorageInfoTestDAO.find(scs);
	}

	@Override
	public List<BasicStapleUnitDO> resultStapleUnit(SearchConditions scs) {
		return basicStapleUnitDAO.find(scs);
	}
	
	@Override
	public List<BasicProcessDO> resultProcess(SearchConditions scs){
		return basicProcessDAO.find(scs);
	}

	@Override
	public List<BasicWorkshopDO> resultWorkshop(SearchConditions scs) {
		return basicWorkshopDAO.find(scs);
	}
	
	@Override
	public List<String> resultWorkshopIdList(List<String> workShopCodeList, SearchConditions scs){
		List<String> workShopIdList = new ArrayList<String>();
		Map<String, String> workShopMap = this.getWorkShopToMap(scs);
		for(String workShopCode : workShopCodeList){
			if(StringUtils.isBlank(workShopCode)){
				continue;
			}
			String workShopId = workShopMap.get(workShopCode);
			if(StringUtils.isNotBlank(workShopId)){
				workShopIdList.add(workShopId);
			}
		}
		return workShopIdList;
	}
	
	private Map<String, String> getWorkShopToMap(SearchConditions scs){
		List<BasicWorkshopDO> workShopList = this.resultWorkshop(scs);
		if(workShopList==null||workShopList.isEmpty()||workShopList.size()==0){
			return null;
		}else{
			Map<String, String> workShopMap = new HashMap<String, String>();
			for(BasicWorkshopDO workShop : workShopList){
				if(workShop==null||StringUtils.isBlank(workShop.getId())){
					continue;
				} 
				workShopMap.put(workShop.getZd001Code(), workShop.getId());
			}
			return workShopMap;
		}
	}

	@Override
	public List<BasicMaterialQualityDO> resultMaterialQuality(
			SearchConditions scs) {
		return basicMaterialQualityDAO.find(scs);
	}

	@Override
	public List<BasicCompanyTestDO> resultCompany(SearchConditions scs) {
		return basicCompanyTestDAO.find(scs);
	}
	
	@Override
	public List<BasicCurrencyTestDO> resultCurrency(SearchConditions scs) {
		return basicCurrencyTestDAO.find(scs);
	}

	@Override
	public List<BasicSeriesDO> resultSeries(SearchConditions scs) {
		return basicSeriesDAO.find(scs);
	}
	
	@Override
	public List<BasicModePaymentTestDO> resultPayments(SearchConditions scs){
		return basicModePaymentTestDAO.find(scs);
	}
}
